import OSS from 'ali-oss'

//这里的配置和上传的配置保持一致，毕竟只是为了查询当前阿里云已存的所有文件url
// const OSSConfig = new OSS({
//   accessKeyId: 'LTAI5tCNGG64r8RzRHSGkEXf',
//   accessKeySecret: 'E0czrHr6787rv3lwDJRMR76ubl6tUy',
//   bucket: 'hxsw-jihuo-applet-meidaojia.oss-cn-beijing.aliyuncs.com',
//   region: 'oss-cn-beijing',
// })
const OSSConfig = {
  uploadHost: 'https://hxsw-jihuo-applet-meidaojia.oss-cn-beijing.aliyuncs.com', // OSS上传地址
  ossParams: {
    accessKeyId: 'LTAI5tCNGG64r8RzRHSGkEXf',
    accessKeySecret: 'E0czrHr6787rv3lwDJRMR76ubl6tUy',
    bucket: 'hxsw-jihuo-applet-meidaojia.oss-cn-beijing.aliyuncs.com',
    region: 'oss-cn-beijing',
  },
  fileDir: '',
}
/**
 * @description 上传图片到oss
 * @param value
 * @returns {boolean}
 */
export function uploadOSSImage(file) {
  console.log('uploadOSSImage', file)
  /* eslint-disable */
  return new Promise(async (resolve, reject) => {
    const fileName = `ProductImage/${file.name}`
    console.log('uploadOSSImage fileName', fileName)
    const client = new OSS({
      region: OSSConfig.ossParams.region,
      accessKeyId: OSSConfig.ossParams.accessKeyId,
      accessKeySecret: OSSConfig.ossParams.accessKeySecret,
      bucket: OSSConfig.ossParams.bucket,
    })
    console.log('uploadOSSImage client', client)
    const res = await client.multipartUpload(fileName, file)
    // resolve(res)
    console.log('上传图片 uploadOSSImage', res)
    // 或者返回如下：
    resolve({
      fileUrl: `${OSSConfig.uploadHost}/${fileName}`,
      fileName: file.name,
    })
  })
}
export function client() {
  var client = new OSS({
    accessKeyId: 'LTAI5tCNGG64r8RzRHSGkEXf',
    accessKeySecret: 'E0czrHr6787rv3lwDJRMR76ubl6tUy',
    bucket: 'hxsw-jihuo-applet-meidaojia',
    region: 'oss-cn-beijing',
    secure: true,
  }) //后端提供数据
  return client
}
/**
 * 生成随机文件名称
 * 规则八位随机字符，加下划线连接时间戳
 */
export const getFileNameUUID = () => {
  function rx() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
  }
  return `${+new Date()}_${rx()}${rx()}`
}
